<template>
  <div id="team">
    <el-form>
      <el-form-item>
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="addTeam">新增团队</el-button>
      </el-form-item>
    </el-form>
    <el-table
      border
      stripe
      :data="teamList"
    >
      <el-table-column type="expand">
        <template v-slot="{ row }">
          <el-table
            :data="row.userList"
            border
            stripe
          >
            <el-table-column type="index" label="序号" width="60"></el-table-column>
            <el-table-column label="用户名" prop="username" align="center"></el-table-column>
            <el-table-column label="密码" prop="password" align="center"></el-table-column>
            <el-table-column label="角色" prop="roleName" align="center"></el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column label="序号" type="index" width="100" align="center"></el-table-column>
      <el-table-column label="团队名称" prop="teamName" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="290">
        <template v-slot="{ row }">
          <el-button type="success" size="mini" icon="el-icon-user" @click="teamUserMana(row)">管理</el-button>
          <el-button type="warning" size="mini" icon="el-icon-brush" @click="cleanTeam(row)">清空团队</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteTeam(row)">删除团队</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :visible.sync="addTeamVisible"
      :title="addTeamDialogTitle"
    >
      <el-form :model="addTeamForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item prop="teamName" label="团队名称">
          <el-input v-model="addTeamForm.teamName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveTeamForm">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addTeam, deleteTeam, listTeam, updateTeam } from '@/api/team'
import { cleanTeam } from '@/api/teamUser'

export default {
  name: 'Index',
  data() {
    return {
      teamList: [],
      addTeamVisible: false,
      addTeamDialogTitle: '添加',
      addTeamForm: {
        teamName: ''
      },
      addFormRules: {
        teamName: [
          { required: true, message: '请输入团队名称', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getTeamList()
  },
  methods: {
    getTeamList() {
      listTeam().then(res => {
        this.teamList = res.data.list
      })
    },
    addTeam() {
      this.addTeamVisible = true
      Object.keys(this.addTeamForm).forEach(key => delete this.addTeamForm[key])
      this.$set(this.addTeamForm, 'teamName', '')
    },
    saveTeamForm() {
      this.$refs.addForm.validate(validate => {
        if (validate) {
          if (this.addTeamDialogTitle === '添加') {
            addTeam(this.addTeamForm).then(res => {
              this.addTeamVisible = false
              if (res.resultCode === 200) {
                this.$message.success('操作成功')
              }
              Object.keys(this.addTeamForm).forEach(key => delete this.addTeamForm[key])
              this.$set(this.addTeamForm, 'teamName', '')
              this.getTeamList()
            })
          } else {
            updateTeam(this.addTeamForm).then(res => {
              this.addTeamVisible = false
              if (res.resultCode === 200) {
                this.$message.success('操作成功')
              }
              Object.keys(this.addTeamForm).forEach(key => delete this.addTeamForm[key])
              this.$set(this.addTeamForm, 'teamName', '')
              this.getTeamList()
            })
          }
        } else {
          return
        }
      })
    },
    updateTeam(row) {
      this.addTeamVisible = true
      this.addTeamDialogTitle = '修改'
      Object.assign(this.addTeamForm, row)
    },
    cleanTeam(row) {
      this.$confirm('清空团队后该团队所有用户将被移除，是否继续', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        cleanTeam(row).then(res => {
          if (res.resultCode === 200) {
            this.$message.success('操作成功')
          }
          this.getTeamList()
        })
      })
    },
    deleteTeam(row) {
      this.$confirm('删除后该团队相关所有数据都将被删除，是否继续', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTeam(row.id).then(res => {
          if (res.resultCode === 200) {
            this.$message.success('操作成功')
          }
          this.getTeamList()
        })
      })
    },
    teamUserMana(row) {
      this.$router.push({
        path: `/team/user/${row.id}`
      })
    }
  }
}
</script>

<style scoped>

</style>
